﻿/***********************************************************************
 * <copyright file="StockController.cs" company="BSD JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Friday, May 16, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Web.Mvc;
using DxMvcApplication.Areas.Administrator.Models.Dictionary;
using DxMvcApplication.Repositories.Implementations.Dictionary;

namespace DxMvcApplication.Areas.Administrator.Controllers.Dictionary
{
    public class StockController : Controller
    {
        private readonly StockRepository _stockRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="StockController"/> class.
        /// </summary>
        public StockController()
        {
            _stockRepository = new StockRepository();
        }

        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {                   
            return View("Stock");
        }

        public ActionResult GetPaged(int? page)
        {
            var pageIndex = page ?? 1;
            var totalPage = _stockRepository.GetPageSize();
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = pageIndex;
            var stocks = _stockRepository.GetPaged(ref totalPage, pageIndex);
            return View("StockList", stocks.ToModels());
        }

        public ActionResult Detail(int? id)
        {
            if(id!=null)
            {                
                var stock = _stockRepository.Get((int) id);
              //  return View("StockDetail", stock.ToModel());
            }
            return View("StockDetail", new StockModel());
        }
        [HttpPost]
        public JsonResult SaveData(StockModel model)
        {
            string msg = null;
            if(ModelState.IsValid)
            {                                
                msg = model.StockId>0 ? _stockRepository.Update(model.FromModel()) : _stockRepository.Insert(model.FromModel());                
            }
            return Json(new { message = msg });
        }
    }
}
